package com.hg.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hg.mapper.ItemCatMapper;
import com.hg.mapper.ItemDescMapper;
import com.hg.mapper.ItemParamItemMapper;
import com.hg.pojo.Item;
import com.hg.mapper.ItemMapper;
import com.hg.pojo.ItemCat;
import com.hg.pojo.ItemDesc;
import com.hg.pojo.ItemParamItem;
import com.hg.service.IItemService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hg.util.IDUtils;
import com.hg.util.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 商品表 服务实现类
 * </p>
 *
 * @author Baomidou
 * @since 2025-07-30
 */
@Service
@Transactional
public class ItemServiceImpl extends ServiceImpl<ItemMapper, Item> implements IItemService {
    @Autowired
    private ItemMapper itemMapper;
    @Autowired
    private ItemDescMapper itemDescMapper;
    @Autowired
    private ItemParamItemMapper itemParamItemMapper;
    @Autowired
    private ItemCatMapper itemCatMapper;

    @Override
    public PageResult selectTbItemAllByPage(Integer page, Integer rows) {
        Page<Item> pageInfo = new Page<>(page, rows);
        QueryWrapper<Item> queryWrapper = new QueryWrapper<>();
        //删除
        queryWrapper.ne("status",3);
        //按更新时间排序
        queryWrapper.orderByDesc("updated");
        Page<Item> itemList = itemMapper.selectPage(pageInfo, queryWrapper);
        return new PageResult(itemList.getSize(), itemList.getPages(), itemList.getRecords());
    }

    @Override
    public void insertTbItem(Item item, String desc, String itemParams) {
        //1、保持商品基本信息
        long itemId = IDUtils.genItemId();
        item.setId(itemId);
        item.setPrice(item.getPrice()*100);
        item.setStatus((byte)1);
        item.setCreated(new Date());
        item.setUpdated(new Date());
        itemMapper.insert(item);
        //2、保持商品描述信息
        ItemDesc itemDesc = new ItemDesc();
        itemDesc.setItemId(itemId);
        itemDesc.setItemDesc(desc);
        itemDesc.setCreated(new Date());
        itemDesc.setUpdated(new Date());
        itemDescMapper.insert(itemDesc);
        //3、保持商品规格参数
        ItemParamItem itemParamItem = new ItemParamItem();
        itemParamItem.setItemId(itemId);
        itemParamItem.setParamData(itemParams);
        itemParamItem.setCreated(new Date());
        itemParamItem.setUpdated(new Date());
        itemParamItemMapper.insert(itemParamItem);
    }

    @Override
    public void deleteItemById(Long itemId) {
//        itemMapper.deleteById(itemId);
//        itemDescMapper.deleteById(itemId);
//        itemParamItemMapper.deleteById(itemId);
        Item item = new Item();
        item.setId(itemId);
        item.setStatus((byte) 3);
        itemMapper.updateById(item);
    }

    @Override
    public void updateTbItem(Item item, String desc, String itemParams) {
        //1、修改商品基本信息
        item.setPrice(item.getPrice() * 100);
        item.setCreated(new Date());
        item.setUpdated(new Date());
        itemMapper.updateById(item);
        //2、修改商品描述信息
        Long itemId = item.getId();
        ItemDesc itemDesc = new ItemDesc();
        itemDesc.setItemId(itemId);
        itemDesc.setItemDesc(desc);
        itemDesc.setCreated(new Date());
        itemDesc.setUpdated(new Date());
        itemDescMapper.updateById(itemDesc);
        //3、修改商品规格参数信息
        ItemParamItem itemParamItem = new ItemParamItem();
        itemParamItem.setParamData(itemParams);
        itemParamItem.setCreated(new Date());
        itemParamItem.setUpdated(new Date());
        QueryWrapper<ItemParamItem> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("item_id", itemId);
        itemParamItemMapper.update(itemParamItem, queryWrapper);
    }

    @Override
    public Map preUpdateItem(Long itemId) {
        Map<String,Object> map = new HashMap();

        Item item = itemMapper.selectById(itemId);
        item.setPrice(item.getPrice()/100);
        map.put("item",item);

        ItemCat itemCat = itemCatMapper.selectById(item.getCid());
        map.put("itemCat",itemCat.getName());

        ItemDesc itemDesc = itemDescMapper.selectById(itemId);
        map.put("itemDesc",itemDesc.getItemDesc());

        QueryWrapper<ItemParamItem> paramItemQueryWrapper = new QueryWrapper<>();
        paramItemQueryWrapper.eq("item_id",itemId);
        ItemParamItem itemParamItem = itemParamItemMapper.selectOne(paramItemQueryWrapper);
        if (itemParamItem != null&&itemParamItem.equals("")){
            map.put("itemParamItem",itemParamItem.getParamData());

        }

        return map;
    }
}
